package com.itheima.demo10JDBC案例;

import com.itheima.utils.JDBCUtils;

import javax.sql.DataSource;
import java.net.DatagramPacket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 品牌数据的增删改查操作
 */
public class BrandTest {
    public static void main(String[] args) throws SQLException {

//        findall();//查询所有数据
//        添加：添加品牌
//        if (insertBrand()){
//            System.out.println("添加成功");
//        }else {
//            System.out.println("添加失败");
//        }
//        修改：根据 id修改
//        if (updateById(3)){
//            System.out.println("修改成功");
//        }else {
//            System.out.println("修改失败");
//        }
//        删除：根据 id删除
           if (deleteById(1)){
               System.out.println("删除成功");
           }else {
               System.out.println("删除失败");
           }
    }

    private static boolean deleteById(int id) throws SQLException {
        Connection connection = JDBCUtils.getDataSource().getConnection();
        String sql ="delete from tb_brand where id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,id);
        int i = ps.executeUpdate();
        JDBCUtils.close(ps,connection);
        if (i>=1){
            return true;
        }else {
            return false;
        }
    }

    private static boolean updateById(int id) throws SQLException {
        DataSource dataSource = JDBCUtils.getDataSource();
        Connection connection = dataSource.getConnection();
        String sql = "update tb_brand set ordered = ? where id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,8);
        ps.setInt(2,id);
        int row = ps.executeUpdate();
        if (row>=1){
            return true;
        }else {
            return false;
        }
    }

    private static boolean insertBrand() throws SQLException {
        DataSource dataSource = JDBCUtils.getDataSource();
        Connection connection = dataSource.getConnection();
        String sql ="insert into tb_brand values(null,?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,"鸿星尔克");
        ps.setString(2,"鸿星尔克实业有限公司");
        ps.setInt(3,1);
        ps.setString(4,"TO BE NO.1");
        ps.setInt(5,1);
        int i = ps.executeUpdate();
        JDBCUtils.close(ps,connection);
        if (i>=1){
            return true;
        }else {
            return false;
        }


    }

    private static void findall() throws SQLException {
        List<Brand> brands = new ArrayList<>();
        DataSource dataSource = JDBCUtils.getDataSource();
        Connection connection = dataSource.getConnection();
        String sql = "select * from tb_brand";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String brand_name = resultSet.getString("brand_name");
            String company_name = resultSet.getString("company_name");
            int ordered = resultSet.getInt("ordered");
            String description = resultSet.getString("description");
            int status = resultSet.getInt("status");

            Brand brand = new Brand(id,brand_name,company_name,ordered,description,status);
            brands.add(brand);
        }
        System.out.println(brands);
        JDBCUtils.close(resultSet,ps,connection);
    }
}